﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Mrc.Application;
using Mrc.Common;
using Mrc.Common.Enum;
using Mrc.Domain.Module.Auth;
 
namespace Mrc.UserApp.Controllers
{
    public class LoginController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        public JsonResult init()
        {
 
            return Json("");
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<JsonResult> Login(string userName, string password)
        {
            var logininfo = await new UserLoginService().CheckLogin(userName, password);
            Dictionary<string, object> payLoad = new Dictionary<string, object>();
            payLoad.Add("sub", "user");
            payLoad.Add("jti", Guid.NewGuid().ToString());
            payLoad.Add("nbf", null);
            payLoad.Add("exp", null);
            payLoad.Add("iss", "Issuser");
            payLoad.Add("aud", "user");
            payLoad.Add("user", logininfo.Name);
            payLoad.Add("isLock", logininfo.State);
            logininfo.Token  = TokenManager.CreateToken(payLoad, 30);     
            var result = AjaxResult.CreateResult(ResultStatus.OK, "登录成功",logininfo);
            HttpContext.Response.Cookies.Append("MRCTOKEN", logininfo.Token, new Microsoft.AspNetCore.Http.CookieOptions
            {
                Expires = DateTime.Now.AddDays(1)
            });
            return Json(result);
        }
    }
}